﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Windows.Media.Imaging;
using PanelComponent;

namespace ImageEditor
{
    public partial class Form1 : Form
    {        
        private Point mouseLocation = new Point(0, 0);
        private bool isPictureMove = false;
        private Image myImage;
        private Rectangle myImageBox;        
        private int rotationAngle = 0;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "image files (*.png)|*.png|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            //PngBitmapDecoder decoder = new PngBitmapDecoder(myStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                            //BitmapSource bitmapSource = decoder.Frames[0];

                            myImage = Image.FromStream(myStream);
                            myImageBox = new Rectangle(new Point(0, 0), myImage.Size);
                            panel1.Invalidate();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }                   

        private void panel1_Paint(object sender, PaintEventArgs e)
        {            
            Graphics g = e.Graphics;           

            if (myImage != null)
            {
                int rotOriginX = myImageBox.X + myImageBox.Width/2;
                int rotOriginY = myImageBox.Y + myImageBox.Height/2;
                g.TranslateTransform(rotOriginX, rotOriginY);
                g.RotateTransform(rotationAngle);
                g.TranslateTransform(-rotOriginX, -rotOriginY);
                
                //g. = myImage.HorizontalResolution;
                //g.DpiY = myImage.VerticalResolution;
                //g.DrawImage(myImage, new Point(myImageBox.X, myImageBox.Y));
                g.DrawImageUnscaledAndClipped(myImage, myImageBox);
                
                // Create a new pen.
                Pen redPen = new Pen(Brushes.Red);

                // Set the pen's width.
                redPen.Width = 2.0F;

                // Set the LineJoin property.
                redPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;

                // Draw a rectangle.
                e.Graphics.DrawRectangle(redPen,
                    myImageBox);

                //Dispose of the pen.
                redPen.Dispose();                
            }
            
            base.OnPaint(e);
        }      

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            rotationAngle = trackBar1.Value;
            panel1.Invalidate();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseLocation = e.Location;                
            
            if (myImageBox.Contains(mouseLocation)) 
            {                
                isPictureMove = true;                
            }                                                        
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {                           
            isPictureMove = false;                             
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
     
            if (isPictureMove)
            {
                                
                int dX = e.X - mouseLocation.X;
                int dY = e.Y - mouseLocation.Y;               
                
                myImageBox.Offset(dX, dY);

                if (myImageBox.X < 0)
                {
                    myImageBox.X = 0;
                }
                else if (myImageBox.X + myImageBox.Width > panel1.Width)
                {
                    myImageBox.X = panel1.Width - myImageBox.Width;
                }

                if (myImageBox.Y < 0)
                {
                    myImageBox.Y = 0;
                }
                else if (myImageBox.Y + myImageBox.Height > panel1.Height)
                {
                    myImageBox.Y = panel1.Height - myImageBox.Height;
                }        
                
                mouseLocation = e.Location;                
                panel1.Invalidate();    
            }       
        }

        

    }
}
